package com.topc.platform.api;

import com.topc.base.api.CommonResult;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.platform.model.po.CarouselInfo;
import com.topc.platform.service.CarouselInfoService;
import com.topc.platform.util.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 轮播图 前端控制器
 * </p>
 *
 * @author 666
 */
@Slf4j
@RestController
@RequestMapping("/close/carousel")
public class CarouselCloseController {

    @Autowired
    private CarouselInfoService  carouselInfoService;

    @Autowired
    private MinioUtils minioUtils;

    //增添图片
    @PostMapping("/save")
    public CommonResult<String> addInfo(@RequestParam("file") MultipartFile file,@RequestParam("linkUrl") @NotEmpty String linkUrl){
        String imgUrl = minioUtils.uploadOne(file);
        carouselInfoService.saveInfo(imgUrl,linkUrl);
        return CommonResult.success("创建成功");
    }
    //删除图片
    @GetMapping("/delete")
    public CommonResult<String> removeInfoById(@NotNull Long id){
        carouselInfoService.removeInfoById(id);
        return CommonResult.success("删除成功");
    }

    //修改图片信息
    @GetMapping("/update-info")
    public CommonResult<String> updateInfoById(@NotNull Long id, @NotEmpty String linkUrl, @Range(min = -1,max = 2) int status){
        carouselInfoService.updateInfoById(id,linkUrl,status);
        return CommonResult.success("修改成功");
    }

    @PostMapping("/update-img")
    public CommonResult<String> updateImgById(@NotNull Long id,@RequestParam("file") MultipartFile file){
        String imgUrl = minioUtils.uploadOne(file);
        Boolean update = carouselInfoService.updateImgById(id,imgUrl);
        if (!update){
            minioUtils.removeOne(imgUrl);
            return CommonResult.failed("修改失败");
        }
        return CommonResult.success("修改成功");
    }

    //分页查询图片信息
    @PostMapping("/search-info")
    public CommonResult<PageResult<CarouselInfo>> searchInfoByPage(PageParams pageParams, @Range(min = -1,max = 2)int status){
        PageResult<CarouselInfo> page=carouselInfoService.searchByPage(pageParams,status);
        return CommonResult.success(page);
    }
}
